let u = navigator.userAgent
/***
 * 浏览器标识
 * @type {{versions: {trident, presto, webKit, gecko, mobile, ios, android, iPhone, iPad, webApp}, language: string}}
 */
let browser = {
    versions: (function() {
        return { // 移动终端浏览器版本信息
            trident: u.indexOf('Trident') > -1, // IE内核
            presto: u.indexOf('Presto') > -1, // opera内核
            webKit: u.indexOf('AppleWebKit') > -1, // 苹果、谷歌内核
            gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') === -1, // 火狐内核
            mobile: !!u.match(/AppleWebKit.*Mobile.*/), // 是否为移动终端
            ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), // ios终端
            android: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, // android终端或者uc浏览器
            iPhone: u.indexOf('iPhone') > -1, // 是否为iPhone或者QQHD浏览器
            iPad: u.indexOf('iPad') > -1, // 是否iPad
            webApp: u.indexOf('Safari') === -1 // 是否web应该程序，没有头部与底部
        }
    })(),
    language: (navigator.browserLanguage || navigator.language).toLowerCase()
}
/***
 * 打印对象
 * @param o
 * @returns {*}
 */
let printObject = function(o) {
    let r = []
    if (typeof o === 'string') {
        return '\'' + o.replace(/([\\])/g, '\\$1').replace(/(\n)/g, '\\n').replace(/(\r)/g, '\\r').replace(/(\t)/g, '\\t') + '\''
    }
    if (typeof o === 'object') {
        if (!o.sort) {
            for (let i in o) {
                r.push('\'' + i + '\'' + ':' + printObject(o[i]))
            }
            if (!!document.all && !/^\n?function\s*toString\(\)\s*\{\n?\s*\[native code\]\n?\s*\}\n?\s*$/.test(o.toString)) {
                r.push('toString:' + o.toString.toString())
            }
            r = '{' + r.join() + '}'
        } else {
            for (let i = 0; i < o.length; i++) {
                r.push(printObject(o[i]))
            }
            r = '[' + r.join() + ']'
        }
        return r
    }
    if (typeof o === 'object') {
        if (!o.sort) {
            for (let i in o) {
                r.push('\'' + i + '\'' + ':' + printObject(o[i]))
            }
            if (!!document.all && !/^\n?function\s*toString\(\)\s*\{\n?\s*\[native code\]\n?\s*\}\n?\s*$/.test(o.toString)) {
                r.push('toString:' + o.toString.toString())
            }
            r = '{' + r.join() + '}'
        } else {
            for (let i = 0; i < o.length; i++) {
                r.push(printObject(o[i]))
            }
            r = '[' + r.join() + ']'
        }
        return r
    }
    return o.toString()
}

export {
    browser,
    printObject
}
